/*-----------------------------------------------------------------------------*/
/* InitDataBase.java                                                           */
/* Program that initializes the ecom databaseby creating beans			 */
/* Fabienne Boyer - Didier Donsez may 2006                                     */
/*-----------------------------------------------------------------------------*/
package ecom.client;

import ecom.beans.cart.*;
import ecom.beans.account.*;
import ecom.beans.customer.*;
import ecom.beans.dish.*;
import ecom.beans.store.*;
import ecom.beans.address.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import javax.ejb.EJB;

import javax.transaction.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class InitDataBase {

    private Context initialContext;
    private UserTransaction utx;
	
	private StoreEntityFacadeRemote storeEntityFacade;
	private DishEntityFacadeRemote dishEntityFacade;
    private CustomerEntityFacadeRemote customerEntityFacade;
	private AccountEntityFacadeRemote accountEntityFacade;
	
	
    public InitDataBase() {

        initialContext = null;
        utx = null;
        dishEntityFacade = null;
        storeEntityFacade = null;
        customerEntityFacade = null;
		accountEntityFacade = null;

        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");

        try {
            initialContext = new InitialContext();
        } catch (Exception e) {
            System.err.println("Cannot get initial context for JNDI: " + e);
            System.exit(2);
        }

        try {
            utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
        } catch (NamingException e) {
            System.err.println("Cannot lookup UserTransaction: " + e);
            System.exit(2);
        }


		try {
            customerEntityFacade = (CustomerEntityFacadeRemote) initialContext.lookup("ecom.beans.customer.CustomerEntityFacade" + "_" + CustomerEntityFacadeRemote.class.getName() + "@Remote");

            System.out.println("Creation of customerEntityFacade...OK");



        } catch (NamingException e) {
            System.err.println("Cannot get customerEntityFacade : " + e);
            System.exit(2);
        }

		

        try {
            storeEntityFacade = (StoreEntityFacadeRemote) initialContext.lookup("ecom.beans.store.StoreEntityFacade" + "_" + StoreEntityFacadeRemote.class.getName() + "@Remote");

            System.out.println("Creation of storeEntityFacade...OK");



        } catch (NamingException e) {
            System.err.println("Cannot get storeEntityFacade : " + e);
            System.exit(2);
        }

        
		
		try {
            dishEntityFacade = (DishEntityFacadeRemote)initialContext.lookup("ecom.beans.dish.DishEntityFacade" + "_" + DishEntityFacadeRemote.class.getName() + "@Remote");
							
			System.out.println("Creation of dishEntityFacade...OK");
			
			

        } catch (NamingException e) {
            System.err.println("Cannot get dishEntityFacade : " + e);
            System.exit(2);
        }
		
		try {
            accountEntityFacade = (AccountEntityFacadeRemote)initialContext.lookup("ecom.beans.account.AccountEntityFacade" + "_" + AccountEntityFacadeRemote.class.getName() + "@Remote");
							
			System.out.println("Creation of accountEntityFacade...OK");
			
			

        } catch (NamingException e) {
            System.err.println("Cannot get accountEntityFacade : " + e);
            System.exit(2);
        }

    }

    public void initDatabase() {

        System.out.println("Initializing Database....");
		System.out.println("--------------------------------");
		System.out.println("Clean DataBase...");
        dishEntityFacade.removeAllDish();
        System.out.println("Remove Dish Records...OK");
		storeEntityFacade.removeAllStore();
        System.out.println("Remove Store Records...OK");
		customerEntityFacade.removeAllCustomers();
		System.out.println("Remove Customer Records...OK");
		System.out.println("--------------------------------");
		
		// ancient code for customer
		/*
		AddressEntity customerAddress1 = new AddressEntity();
		customerAddress1.setAddressType(1);
		customerAddress1.setStreetNum(1);
		customerAddress1.setStreetName("Berlioz");
		customerAddress1.setBuildingNum(1);
		customerAddress1.setFloorNum(1);
		customerAddress1.setApartmentNum(1);
		customerAddress1.setZipcode("12345");
		customerAddress1.setCity("Grenoble");
		
		AccountEntity customerAccount1 = new AccountEntity();
		customerAccount1.setAccountIBAN("FR01");
		customerAccount1.setAccountType(1);
		
		CustomerEntity customer1 = customerEntityFacade.registerCustomer("a","a@a.a","firstname1","lastname1","123",customerAddress1,customerAccount1);
		
		if(customer1 != null)
			System.out.println("add a new customer...OK");
		
		accountEntityFacade.deposit("FR01", 100.5);
		accountEntityFacade.deposit("FR01", 25.0);
		
		*/
		
		CustomerEntity customer1 = null;
		CustomerEntity customer2 = null;
		CustomerEntity customer3 = null;
		CustomerEntity customer4 = null;
		
		AddressEntity address_customer1 = new AddressEntity(1, 12, "des Alouettes", 1, 3, 5, "38000", "Grenoble");
        AccountEntity account_customer1 = new AccountEntity("FR7630003003930003728602468", 95.7, 1);
        customer1 = customerEntityFacade.registerCustomer("a", "a@a.com", "Robert", "Garcia", "01234", address_customer1, account_customer1);
        if(customer1 != null)
			System.out.println("add a new customer...OK");
		else
			System.out.println("can not add a new customer");
		
        AddressEntity address_customer2 = new AddressEntity(1, 45, "Charles de Gaulle", -1, -1, -1, "54621", "Rubigny les oies");
        AccountEntity account_customer2 = new AccountEntity("FR3514", 43.1, 1);
        customer2 = customerEntityFacade.registerCustomer("b", "b@b.com", "Tatiana", "Girard", "0789543205", address_customer2, account_customer2);
		if(customer1 != null)
			System.out.println("add a new customer...OK");
		else
			System.out.println("can not add a new customer");
        
		
        AddressEntity address_customer3 = new AddressEntity(1, 1004, "de la Republique", 4, 38, 675, "11400", "Blabla");
        AccountEntity account_customer3 = new AccountEntity("FR008", 13.5, 1);
        customer3 = customerEntityFacade.registerCustomer("c", "c@c.com", "Stephane", "Dupont", "987456", address_customer3, account_customer3);
		if(customer1 != null)
			System.out.println("add a new customer...OK");
		else
			System.out.println("can not add a new customer");
        
		
		
        AddressEntity address_customer4 = new AddressEntity(1, 39, "Etoile Celeste", -1, -1, -1, "75001", "Paris");
        AccountEntity account_customer4 = new AccountEntity("FR123", 1150.68, 1);
        customer4 = customerEntityFacade.registerCustomer("star", "jjgoldman@sacem.com", "Jean-Jacques", "Goldman", "0999999999", address_customer4, account_customer4);
		if(customer1 != null)
			System.out.println("add a new customer...OK");
		else
			System.out.println("can not add a new customer");

		System.out.println("\n[TOTAL : 4 Customers]");
		System.out.println("--------------------------------");

		/**********************************************************/
		
		// ancient code for store
		/*
		
		AddressEntity address2 = new AddressEntity();
		address2.setAddressType(2);
		address2.setStreetNum(5);
		address2.setStreetName("Campus");
		address2.setBuildingNum(2);
		address2.setFloorNum(2);
		address2.setApartmentNum(2);
		address2.setZipcode(789654);
		address2.setCity("DU");
		
		AccountEntity account2 = new AccountEntity();
		account2.setAccountIBAN("FR02");
		account2.setAccountType(2);
		
		StoreEntity newStore = storeEntityFacade.addStore("resto1",100,10,address2,account2);
		accountEntityFacade.deposit("FR02", 500);
		
		if(newStore != null)
			System.out.println("add a new resto...OK");
		
		
		DishEntity dish1 = dishEntityFacade.addDish("plat1", 2.5, 10, "type1", newStore);
		if(dish1 != null)	
			System.out.println("add a new plat...OK");
		
		DishEntity dish2 = dishEntityFacade.addDish("plat2", 5.5, 10, "type1", newStore);
		if(dish2 != null)
			System.out.println("add a new plat...OK");
		
		DishEntity dish3 = dishEntityFacade.addDish("plat3", 9, 10, "type1", newStore);
		if(dish3 != null)
			System.out.println("add a new plat...OK");
		
		*/
		
		StoreEntity store1 = null;
		
		AddressEntity address_resto1 = new AddressEntity(2, 98, "du commerce central", -1, -1, -1, "38400", "Saint Martin d'Heres");
        AccountEntity account_resto1 = new AccountEntity("FR6517203003936303728600000", 10599.6, 2);
        store1 = storeEntityFacade.addStore("SpeedCooking", 86, 7, address_resto1, account_resto1);
		if(store1 != null)
			System.out.println("add a new resturant...OK");
		else
			System.out.println("can not add a new resturant");
			
		System.out.println("\n[TOTAL : 1 Resturant]");
		
		System.out.println("--------------------------------");
		
		
		/**********************************************************/
		
		
		DishEntity dish = null;
		
		dish = dishEntityFacade.addDish("Cheeseburger", 2.2, 43, "Fastfood",300,store1);
		
		
		if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
	
		dish = null;
		
		
        dish = dishEntityFacade.addDish("Fishburger", 2.5, 36, "Fastfood", 305, store1);
		if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
			
		
		
		dish = null;
		
        dish = dishEntityFacade.addDish("Chicken Nuggets", 2.5, 36, "Fastfood", 305, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Big Boy Burger", 3.4, 65, "Fastfood", 520, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		
        dish = dishEntityFacade.addDish("Portion de frites", 1.8, 105, "Accompagnement", 350, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Portion de riz blanc", 1.2, 84, "Accompagnement", 130, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Portion de riz cantonnais", 1.5, 73, "Accompagnement", 160, store1);
        
        dishEntityFacade.addDish("Lasagnes", 4.6, 32, "Plat classique", 430, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Lentilles saucisses", 4.0, 14, "Plat classique", 388, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Filet mignon d'agneau avec petits legumes", 5.2, 26, "Plat classique", 372, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		
        dish = dishEntityFacade.addDish("Diet Cola", 1.2, 123, "Boisson", 15, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		
		dish = dishEntityFacade.addDish("Jus d'orange", 1.2, 201, "Boisson", 45, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Eau minerale", 1, 168, "Boisson", 0, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		
		dish = dishEntityFacade.addDish("Charlotte aux fraises", 2.3, 29, "Dessert", 287, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		dish = dishEntityFacade.addDish("Salade de fruits frais", 1.99, 95, "Dessert", 164, store1);
        if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		dish = null;
		
		
		dish = dishEntityFacade.addDish("Mousse au chocolat", 2.19, 1, "Dessert", 360, store1);
		if(dish != null)
			System.out.println("add a new plat...OK");
		else
			System.out.println("can not add a new plat");
		
		System.out.println("\n[TOTAL : 16 plats]");
		
		System.out.println("--------------------------------");
		
		System.out.println("\n[Init DataBase]...OK");
		
    }
    

    public static void main(String[] args) throws Exception {


        InitDataBase init = new InitDataBase();

        try {

            //init all the database
            init.utx.begin();

			// normally we can choose wether we should init dadabase or clean the database or not !!!
			
            init.initDatabase();
           

            init.utx.commit();

        } catch (Exception e) {

            System.err.println("InitDataBase program get an exception " + e);
            init.utx.rollback();
            System.exit(2);
        }
    }
}

